table(bes22rdd_con$date_diff)
#Data Subset for Each PID X Variable Combinations
bes22rdd_con <- bes_w22 |> filter(partyId==1)
bes22rdd_lab <- bes_w22 |> filter(partyId==2)
bes22rdd_ind <- bes_w22 |> filter(partyId==10)

# Restrict outcomes to their original response scales to drop invalid/missing responses
bes22rdd_all_covid <- bes_w22 |> 
  filter(handleCorona<=5)
bes22rdd_con_covid <- bes22rdd_con |> 
  filter(handleCorona<=5)
bes22rdd_lab_covid <- bes22rdd_lab |> 
  filter(handleCorona<=5)
bes22rdd_ind_covid <- bes22rdd_ind |> 
  filter(handleCorona<=5)

bes22rdd_all_lockdown <- bes_w22 |> 
  filter(govtHandlelockdown<=5)
bes22rdd_con_lockdown <- bes22rdd_con |> 
  filter(govtHandlelockdown<=5)
bes22rdd_lab_lockdown <- bes22rdd_lab |> 
  filter(govtHandlelockdown<=5)
bes22rdd_ind_lockdown <- bes22rdd_ind|> 
  filter(govtHandlelockdown<=5)

bes22rdd_all_likecon <- bes_w22 |> 
  filter(likeCon<=10)
bes22rdd_con_likecon <- bes22rdd_con |> 
  filter(likeCon<=10)
bes22rdd_lab_likecon <- bes22rdd_lab |> 
  filter(likeCon<=10)
bes22rdd_ind_likecon <- bes22rdd_ind |> 
  filter(likeCon<=10)

bes22rdd_all_likejohnson <- bes_w22 |> 
  filter(likeJohnson<=10)
bes22rdd_con_likejohnson <- bes22rdd_con |> 
  filter(likeJohnson<=10)
bes22rdd_lab_likejohnson <- bes22rdd_lab |> 
  filter(likeJohnson<=10)
bes22rdd_ind_likejohnson <- bes22rdd_ind |> 
  filter(likeJohnson<=10)

#Function for RD Plots
make_rdplot <- function(y, x, ylab, y_label_pos) {
  
  p <- rdplot(y, x |> as.numeric(), p = 1)
  
  df <- p$vars_bins |>
    select(rdplot_mean_x, rdplot_mean_y, rdplot_se_y) |>
    mutate(
      ll = rdplot_mean_y - 1.96 * rdplot_se_y,
      ul = rdplot_mean_y + 1.96 * rdplot_se_y,
      Treatment = factor(ifelse(rdplot_mean_x >= 0, 1L, 0L))
    )
  
  g <- df |>
    ggplot() +
    geom_point(aes(rdplot_mean_x, rdplot_mean_y), size = 4) +
    geom_vline(xintercept = -0.5) +
    geom_vline(xintercept = -7.5, linetype = 2) +
    geom_smooth(
      aes(rdplot_mean_x, rdplot_mean_y,
          color = Treatment, linetype = Treatment),
      method = "lm", se = FALSE
    ) +
    geom_label(aes(x = -8, y = y_label_pos, label = "First Coverage"), size = 8) +
    geom_label(aes(x = 0,  y = y_label_pos, label = "Video Released"), size = 8) +
    labs(x = "Date Difference", y = ylab) +
    theme_light() +
    theme(text = element_text(size = 28),
          panel.grid = element_blank())
  
  print(g)
}

## All respondents
make_rdplot(
  bes22rdd_all_covid$handleCorona,
  bes22rdd_all_covid$date_diff,
  "COVID-19 Performance",
  2.9
)

make_rdplot(
  bes22rdd_all_lockdown$govtHandlelockdown,
  bes22rdd_all_lockdown$date_diff,
  "Lockdown Performance",
  2.8
)

make_rdplot(
  bes22rdd_all_likejohnson$likeJohnson,
  bes22rdd_all_likejohnson$date_diff,
  "Like Johnson",
  4.1
)

make_rdplot(
  bes_w22$convote,
  bes_w22$date_diff,
  "Vote Intention: Con",
  0.35
)

make_rdplot(
  bes_w22$labvote,
  bes_w22$date_diff,
  "Vote Intention: Lab",
  0.4
)

#Conservative Supporters
make_rdplot(
  bes22rdd_con_covid$handleCorona,
  bes22rdd_con_covid$date_diff,
  "COVID-19 Performance",
  3.7
)

make_rdplot(
  bes22rdd_con_lockdown$govtHandlelockdown,
  bes22rdd_con_lockdown$date_diff,
  "Lockdown Performance",
  3.5
)

make_rdplot(
  bes22rdd_con_likejohnson$likeJohnson,
  bes22rdd_con_likejohnson$date_diff,
  "Like Johnson",
  6.7
)

make_rdplot(
  bes22rdd_con$convote,
  bes22rdd_con$date_diff,
  "Vote Intention: Con",
  0.8
)

make_rdplot(
  bes22rdd_con$labvote,
  bes22rdd_con$date_diff,
  "Vote Intention: Lab",
  0.1
)

#Labours
make_rdplot(
  bes22rdd_lab_covid$handleCorona,
  bes22rdd_lab_covid$date_diff,
  "COVID-19 Performance",
  2.2
)

make_rdplot(
  bes22rdd_lab_lockdown$govtHandlelockdown,
  bes22rdd_lab_lockdown$date_diff,
  "Lockdown Performance",
  2.4
)

make_rdplot(
  bes22rdd_lab_likejohnson$likeJohnson,
  bes22rdd_lab_likejohnson$date_diff,
  "Like Johnson",
  2.1
)

make_rdplot(
  bes22rdd_lab$convote,
  bes22rdd_lab$date_diff,
  "Vote Intention: Con",
  0.06
)

make_rdplot(
  bes22rdd_lab$labvote,
  bes22rdd_lab$date_diff,
  "Vote Intention: Lab",
  0.9
)

#Independents
make_rdplot(
  bes22rdd_ind_covid$handleCorona,
  bes22rdd_ind_covid$date_diff,
  "COVID-19 Performance",
  2.6
)

make_rdplot(
  bes22rdd_ind_lockdown$govtHandlelockdown,
  bes22rdd_ind_lockdown$date_diff,
  "Lockdown Performance",
  2.8
)

make_rdplot(
  bes22rdd_ind_likejohnson$likeJohnson,
  bes22rdd_ind_likejohnson$date_diff,
  "Like Johnson",
  3.4
)

make_rdplot(
  bes22rdd_ind$convote,
  bes22rdd_ind$date_diff,
  "Vote Intention: Con",
  0.2
)

make_rdplot(
  bes22rdd_ind$labvote,
  bes22rdd_ind$date_diff,
  "Vote Intention: Lab",
  0.25
)

